/*
 * Copyright 2004-2005 asoft
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）* 复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.asoft.cn/licenses
 */

package com.asoft.common.logs.service;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.asoft.common.base.web.service.AbstractListService;
import com.asoft.common.logs.manager.OperatingLogManager;
import com.asoft.common.logs.view.foreveryone.OperatingLogList4EveryOne;

/**
 * <p>Title: 操作日志 - 查询操作日志</p>
 * <p>Description:
 *      结果列表
 * </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: asoft</p>
 * @ $Author: author $
 * @ $Date: 2005/02/15 09:14:59 $
 * @ $Revision: 1.7 $
 * @ created in 2005-11-14
 *
 */

public class FindOperatingLogsService extends AbstractListService
{
        static Logger logger = Logger.getLogger(FindOperatingLogsService.class);

        private OperatingLogManager operatingLogManager;

        public void setOperatingLogManager(OperatingLogManager operatingLogManager)
        {
                this.operatingLogManager = operatingLogManager;
        }

        /** 执行服务 */
        public List getList(HttpServletRequest request)
        {
                // 获取翻页结果集合
                logger.debug("1. 获取查询条件.....");
                // 获取查询条件
                String moduleId = "%";
                if (request.getParameter("moduleId") != null)
                        moduleId = request.getParameter("moduleId");

                String modelId = "%";
                if (request.getParameter("modelId") != null)
                        modelId = request.getParameter("modelId");

                String serviceId = "%";
                if (request.getParameter("serviceId") != null)
                        serviceId = request.getParameter("serviceId");

                String result = "%";
                if (request.getParameter("result") != null)
                        result = request.getParameter("result");

                String operatorName = "%";
                if (request.getParameter("operatorName") != null)
                        operatorName = request.getParameter("operatorName");

                String detail = "%";
                if (request.getParameter("detail") != null)
                        detail = request.getParameter("detail");
                logger.debug("detail: " + detail);
                Timestamp startTime;
                if (request.getParameter("startTime") != null)
                {
                        try
                        {
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                startTime = new Timestamp(sdf.parse(request.getParameter("startTime")).getTime());
                        }
                        catch(ParseException pe)
                        {
                                logger.error("startTime转换Timestamp失败：" + pe.toString());
                                startTime = new Timestamp((new Date()).getTime());
                                startTime.setHours(0);
                                startTime.setMinutes(0);
                        }
                }
                else
                {
                        startTime = new Timestamp((new Date()).getTime());
                        startTime.setHours(0);
                        startTime.setMinutes(0);
                }

                Timestamp endTime;
                if (request.getParameter("endTime") != null)
                {
                        try
                        {
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                endTime = new Timestamp(sdf.parse(request.getParameter("endTime")).getTime());
                                endTime.setDate(endTime.getDate() + 1);
                        }
                        catch(ParseException pe)
                        {
                                logger.error("endTime转换Timestamp失败：" + pe.toString());
                                endTime = new Timestamp((new Date()).getTime());
                                endTime.setDate(endTime.getDate() + 1);
                                endTime.setHours(0);
                                endTime.setMinutes(0);
                        }
                }
                else
                {
                         endTime = new Timestamp((new Date()).getTime());
                         endTime.setDate(endTime.getDate() + 1);
                         endTime.setHours(0);
                         endTime.setMinutes(0);
                }

                String orderBy = "operatingTime";
                if (request.getParameter("orderBy") != null)
                        orderBy = request.getParameter("orderBy");

                String sortType = "desc";
                if (request.getParameter("sortType") != null)
                        sortType = request.getParameter("sortType");
                logger.debug("2. 获取查询条件完毕,开始查询.....");
                List rs = this.operatingLogManager.findOperatingLogs(moduleId,
                                                                         modelId,
                                                                         serviceId,
                                                                         result,
                                                                         "%" + operatorName + "%",
                                                                         detail,
                                                                         startTime,
                                                                         endTime,
                                                                         orderBy,
                                                                         sortType);
                return rs;
        }

        /** model -> viewer */
        public Object getViewer(HttpServletRequest request)
        {
                // 根据request里面提供的参数做角色、权限判断,然后返回对应的viewer
                return new OperatingLogList4EveryOne(); // foreveryone
        }
}

